package com.yss.reportworld.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.constant.Constants;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.reportworld.service.*;
import com.yss.reportworld.util.TableInfoUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Table;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 表信息Controller
 *
 * @author yss
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/table")
public class TableController extends BaseController {
    @Autowired
    private ITableService tableService;
    @Autowired
    private IModularService modularService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询表信息列表
     */
    @RequiresPermissions("reportworld:table:list")
    @GetMapping("/list")
    public TableDataInfo list(Table table) {
        startPage();
        List<Table> list = tableService.selectTableList(table, true);
        return getDataTable(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:table:list")
    @Log(title = "表明细信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody Table table) {
        try {
            AjaxResult ajax = AjaxResult.success();
            table.setStatus(YssStatusEnum.NORMAL.getCode());
            ajax.put("tables", tableService.selectTableList(table, false));
            //加载所属模块，客户，报表
            TableInfoUtil.handleTableVersion(table.getHisVersion()); // 替换表名
            ajax.put("modularTree", modularService.selectModularTreeList());
            ajax.put("customerTree", customerService.selectCustomerTreeList());
            ajax.put("reportTree", reportService.selectReportTreeList());
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }

    }

    /**
     * 导出表信息列表
     */
    @RequiresPermissions("reportworld:table:export")
    @Log(title = "表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Table table) {
        List<Table> list = tableService.selectTableList(table, true);
        tableService.exportExeclTableInfo(response, list);
    }

    /**
     * 获取表信息详细信息
     */
    @RequiresPermissions("reportworld:table:query")
    @PostMapping(value = "/getTable")
    public AjaxResult getTable(@RequestBody Table table) {
        return success(tableService.selectTableById(table));
    }

    /**
     * 新增表信息
     */
    @RequiresPermissions("reportworld:table:add")
    @Log(title = "表信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Table table) {
        tableService.addTable(table);
        return success();
    }

    /**
     * 修改表信息
     */
    @RequiresPermissions("reportworld:table:edit")
    @Log(title = "表信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Table table) {
        tableService.editTable(table);
        return success();
    }

    /**
     * 删除表信息
     */
    @RequiresPermissions("reportworld:table:remove")
    @Log(title = "表信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Table table) {
        return toAjax(tableService.deleteTable(table));
    }

    /**
     * 批量修改表信息
     */
    @RequiresPermissions("reportworld:table:batchEdit")
    @Log(title = "表信息", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateBatch")
    public AjaxResult handleUpdateBatch(@RequestBody Table table) {
        AjaxResult ajax = AjaxResult.success();
        tableService.batchUpdateTableInfo(table);
        return ajax;
    }

    /**
     * 导出建表信息
     */
    @RequiresPermissions("reportworld:table:create")
    @Log(title = "表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/tableCreateInfo")
    public void tableCreateInfo(HttpServletResponse response, String queryJson) throws IOException {
        String sql = tableService.tableCreateInfo(queryJson);
        IOUtils.write(sql, response.getOutputStream(), Constants.UTF8);
    }

    /**
     * 获取一张表信息到库里
     */
    @RequiresPermissions("reportworld:table:readTableInfo")
    @Log(title = "表信息", businessType = BusinessType.OTHER)
    @PostMapping("/readTableInfo")
    public AjaxResult readTableInfo(@RequestBody Table table) {
        AjaxResult ajax = AjaxResult.success();
        tableService.readTableInfo(table);
        return ajax;
    }

    /**
     * 读取设计文档  同步字典和业务主键
     */
    @RequiresPermissions("reportworld:table:readDemandDesign")
    @Log(title = "表信息", businessType = BusinessType.IMPORT)
    @PostMapping("/readDemandDesign")
    public AjaxResult readDemandDesign(MultipartFile file, boolean updateSupport, String hisVersion) throws Exception {
        return success(tableService.readDemandDesign(file, updateSupport, hisVersion));
    }

}


